package com.dreamer.service.mobile.impl;

import com.dreamer.domain.account.GoodsAccount;
import com.dreamer.domain.mall.goods.Goods;
import com.dreamer.domain.user.MutedUser;
import com.dreamer.repository.mobile.GoodsDao;
import com.dreamer.service.mobile.GoodsAccountHandler;
import com.dreamer.service.mobile.GoodsHandler;
import com.dreamer.service.mobile.MuteUserHandler;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Order;
import org.omg.CORBA.portable.ApplicationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ps.mx.otter.utils.SearchParameter;

import java.util.List;
import java.util.Map;

/**
 * Created by huangfei on 03/07/2017.
 */
@Service
public class GoodsHandlerImpl extends BaseHandlerImpl<Goods> implements GoodsHandler {

    @Override
    public List<Goods> findGoods(SearchParameter<Goods> parameter) {
        Example example = Example.create(parameter.getEntity());
        DetachedCriteria criteria = DetachedCriteria.forClass(Goods.class);
        criteria.add(example);
        criteria.addOrder(Order.asc("order"));
        return goodsDao.searchByPage(parameter, criteria);
    }


    @Override
    @Transactional
    public void addBalance(Integer gid, Integer quantity) {
        Goods goods = goodsDao.get(gid);
        goods.increaseCurrentBalance(quantity);
        goodsDao.merge(goods);
    }

    @Override
    @Transactional
    public void reduceBalacne(Integer gid, Integer quantity) {
        Goods goods = goodsDao.get(gid);
        goods.deductCurrentBalance(quantity);
        goodsDao.merge(goods);
    }

    @Override
    @Transactional
    public void addStock(Integer gid, Integer quantity) {
        Goods goods = goodsDao.get(gid);
        goods.increaseCurrentStock(quantity);
        goodsDao.merge(goods);
    }

    @Override
    @Transactional
    public void reduceStock(Integer gid, Integer quantity) {
        Goods goods = goodsDao.get(gid);
        goods.deductCurrentStock(quantity);
        goodsDao.merge(goods);
    }

    @Override
    @Transactional
    public void addSumStock(Integer gid, Integer quantity) {
        Goods goods = goodsDao.get(gid);
        goods.addSumStock(quantity);
        goodsDao.merge(goods);
    }

    @Override
    @Transactional
    public void reduceSumStock(Integer gid, Integer quantity) {
        Goods goods = goodsDao.get(gid);
        goods.reduceSumStock(quantity);
        goodsDao.merge(goods);
    }


    @Override
    @Transactional(rollbackFor = ApplicationException.class)
    public void adminAddSumStock(Integer gid, Integer quantity) {
        MutedUser mutedUser = muteUserHandler.getMuteUser();
        Goods goods  = goodsHandler.get(gid);
        GoodsAccount goodsAccount = goodsAccountHandler.getGoodsAccount(mutedUser,goods,goodsAccountHandler.getMainGoodsAccount(mutedUser));
        if(quantity>0){
            //增加总入库
            goodsHandler.addSumStock(gid,quantity);
            //增加库房库存
            goodsHandler.addStock(gid,quantity);
            //增加公司余额
            goodsAccountHandler.increaseGoodsAccount(goodsAccount,quantity);
            goodsAccountHandler.merge(goodsAccount);
        }else {//减少操作
            //减少总库存
            goodsHandler.reduceSumStock(gid,-quantity);
            //减少库房库存
            goodsHandler.reduceStock(gid,-quantity);
            //减少公司余额  公司账户余额为空
//            goodsAccountHandler.deductGoodsAccount(goodsAccount,quantity);
            goodsAccountHandler.merge(goodsAccount);
        }
    }

    @Override
    public Map<Integer, Integer> sumAdminBalance() {
        return null;
    }

    private GoodsDao goodsDao;

    public GoodsDao getGoodsDao() {
        return goodsDao;
    }

    @Autowired
    private GoodsHandler goodsHandler;

    @Autowired
    private GoodsAccountHandler goodsAccountHandler;
    
    @Autowired
    private MuteUserHandler muteUserHandler;

    @Autowired
    public void setGoodsDao(GoodsDao goodsDao) {
        this.goodsDao = goodsDao;
        super.setBaseDao(goodsDao);
    }
}
